package com.sharemarking.application.common.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sharemarking.application.common.BaseSpecification;
import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.entity.Note;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.NoteRepo;

@Controller
@RequestMapping("note")
public class NoteController {
	

	@Autowired
	private NoteRepo nr;
	
	@Autowired
	private EmployeeRepo er;
	
	
	/**
	 * 便签数据列表
	 * 
	 * @param e
	 * @param page
	 * @param size
	 * @param model
	 */
	@GetMapping(value ="index")
	@PreAuthorize("hasPermission('sys:note',1)")
	public void index(Note e, Integer page, Integer size, Model model) {
		if (page == null) {
			page = 0;
		} else {
			page -= 1;
		}
		if (size == null) {
			size = 10;
		}
		// 按id降序排列
		Sort sort = new Sort(Direction.DESC, "id");
		PageRequest req = new PageRequest(page, size, sort);
		Page<Note> p = nr.findAll(new BaseSpecification<Note>(e),req);
		model.addAttribute("page", p);
		model.addAttribute("search", e);

	}

	/**
	 * 获取实体添加,编辑接口
	 * 
	 * @param obj
	 * @param model
	 * @return
	 */
	@GetMapping(value = "{id}")
	@PreAuthorize("hasPermission('sys:note',1)")
	public String get(Note obj, Model model) {
		if (obj.getId() != 0)
			obj = nr.findOne(obj.getId());
		model.addAttribute("obj", obj);

		return "note/form";
	}

	/**
	 * 便签添加和编辑
	 * 
	 * @param target
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "{id}")
	@Transactional
	@PreAuthorize("hasPermission('sys:note',#target.id==0?8:4)")
	public String index(Note target,HttpServletRequest request) throws IOException {

			// 添加
			if (target.getId() == 0) {
				
				//关联关系
				Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
				
				Employee e=er.findOne(employeeId);
				target.setEmployee2(e);
				
				System.out.println("employeeId:"+employeeId);
				
				nr.save(target);

				return "redirect:index";
			}
			// 编辑
			else {
				Note note = nr.findOne(target.getId());
				if (note == null) {
					return null;
				}
				
				//关联关系
				Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
				
				Employee e=er.findOne(employeeId);
//				target.setEmployee2(e);
				
				System.out.println("employeeId:"+employeeId);
				
				note.setEmployee2(e);
				note.setCreateTime(target.getCreateTime());
				note.setContent(target.getContent());
				
			}
		return "redirect:index";
	}

	/**
	 * 删除便签信息
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "{id}/delete")
	@Transactional
//	@PreAuthorize("hasPermission('sys:note',16)")
	public String delete(@PathVariable("id") Integer id) {
		nr.findOne(id).setDelFlag(true);
		return "redirect:../index";
	}
	 

}
